/*
 * Activiti Modeler component part of the Activiti project
 * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

var KisBpmMulticollectionCtrl = [ '$scope', '$modal', function($scope, $modal) {

    // Config for the modal window
    var opts = {
        template:  'editor-app/user-configuration/properties/multi-collection-popup.html?version=' + Date.now(),
        scope: $scope
    };

    // Open the dialog
    $modal(opts);
}];

var KisBpmMulticollectionPopupCtrl = [ '$scope', '$modal', function($scope, $modal) {
	
    if ($scope.property.value !== undefined && $scope.property.value !== null && $scope.property.value != ''
		&& $scope.property.value.assigneeType !== undefined && $scope.property.value.assigneeType !== null) 
    {
		var value = $scope.property.value;
		var pattern =new RegExp('\\((.| )+?\\)','igm');
		var assigneeDesc = value.assigneeDesc.match(pattern);
		$scope.assignment = {
    		assigneeType : value.assigneeType,
    		assigneeValue : value.assigneeValue,
    		assigneeDesc : value.assigneeDesc.replace(assigneeDesc, '')
    	};
		       
    } 
	else 
	{
        $scope.assignment = {
    		assigneeType : "ROLE",
    		assigneeValue : "",
    		assigneeDesc : ""
    	};
    }
 
 	var w = window.document.body.getWidth();
	var h = window.document.body.getHeight();
	$scope._width = window.document.body.getWidth()*0.4;
	$scope._height = h*0.3;
 
    $scope.save = function() {
		$scope.property.value = {};
		if($scope.assignment.assigneeValue !== undefined && $scope.assignment.assigneeValue !=="" ){
			$scope.property.value = $scope.assignment.assigneeType + "_" + $scope.assignment.assigneeValue;
			$scope.updatePropertyInModel($scope.property);
		}
		$scope.close();
    };

    // Close button handler
    $scope.close = function() {
    	$scope.property.mode = 'read';
    	$scope.$hide();
    };
    
    //添加选择用户、角色、用户组详情
    $scope.showDetail = function(){
    	var templateUrl = "";
    	var selectedValue = $scope.assignment.assigneeType;

    	if(selectedValue){
    		if(selectedValue == "#USER"){
    			templateUrl = "editor-app/user-configuration/properties/choose-user-popup.html";
    		}else if(selectedValue == "ROLE"){
    			templateUrl = "editor-app/user-configuration/properties/choose-role-popup.html";
    		}else if(selectedValue == "GROUP"){
    			templateUrl = "editor-app/user-configuration/properties/choose-group-popup.html";
    		}else{
    			templateUrl = "";
    		}
    	}
	    var opts = {
	        template:  templateUrl + '?version=' + Date.now(),
	        scope: $scope
	    };
	    if(templateUrl!=""){
	    	$modal(opts);
	    }
    }
    
    //改变Type值时Value清空
    $scope.changeType = function(){
    	$scope.assignment.assigneeValue = "";
    	$scope.assignment.assigneeDesc = "";
    }
}];

function selectorData($scope, frameId) {
	var selectorFrame = jQuery(frameId);
	$scope.selectedData = selectorFrame[0].contentWindow.Ext.rtnData;
	$scope.assignment.assigneeValue = $scope.selectedData.assignCode;
	$scope.assignment.assigneeDesc = $scope.selectedData.assignName;
	$scope.assignment.selected = $scope.selectedData;
	$scope.updatePropertyInModel($scope.assignment);
	$scope.close();
}

//提供关于user的取值控制
var UserPopupCtrl = ['$scope', '$modal', '$http', function ($scope, $modal, $http) {
	$scope.property.assignmentName = "用户";

	var w = window.document.body.getWidth();
	var h = window.document.body.getHeight();
	$scope._width = window.document.body.getWidth() * 0.6;
	$scope._widthGrid = window.document.body.getWidth() * 0.6 - 40;
	$scope._height = h * 0.5;

	$scope.save = function () {
		selectorData($scope, "#user-selector-frame");
	};

	$scope.cancel = function () {
		$scope.$hide();
	};

	$scope.close = function () {
		$scope.$hide();
	};
}];

//提供关于role的取值控制
var RolePopupCtrl = ['$scope', '$modal', '$http', function ($scope, $modal, $http) {
	$scope.property.assignmentName = "角色";

	var w = window.document.body.getWidth();
	var h = window.document.body.getHeight();
	$scope._width = window.document.body.getWidth() * 0.6;
	$scope._widthGrid = window.document.body.getWidth() * 0.6 - 40;
	$scope._height = h * 0.5;

	$scope.save = function () {
		selectorData($scope, "#custom-selector-frame");
	};

	$scope.cancel = function () {
		$scope.$hide();
	};

	$scope.close = function () {
		$scope.$hide();
	};
}];

//提供关于group的取值控制
var UsergroupPopupCtrl = ['$scope', '$modal', '$http', function ($scope, $modal, $http) {
	$scope.property.assignmentName = "用户组";

	var w = window.document.body.getWidth();
	var h = window.document.body.getHeight();
	$scope._width = window.document.body.getWidth() * 0.6;
	$scope._widthGrid = window.document.body.getWidth() * 0.6 - 40;
	$scope._height = h * 0.5;

	$scope.save = function () {
		selectorData($scope, "#group-selector-frame");
	};

	$scope.cancel = function () {
		$scope.$hide();
	};

	$scope.close = function () {
		$scope.$hide();
	};
}];